<?php

class AdminUsersController extends AdminAppController {
	
	public $name = 'Users';
	
	public $uses = array('User');
	
	public function index() {
		$this->User->recursive = 0;
		$this->paginate = array (
			'order' => array('User.id' => 'desc')
		);
		$this->set('users', $this->paginate());
	}
	
	public function create() {
		
		if( $this->request->is('ajax') ) {
			if( $this->User->save( $this->request->data ) ) {
				$this->User->recursive = 0;
				$user = $this->User->read();
				$result = array(
					'status' => 1,
					'result' => $user
				);
			}
			else {
				$result = array(
					'status' => 0,
					'error' => $this->User->validationErrors
				);
			}
			
			$this->header("Content-type: application/json");
			$this->autoRender = false;
			$this->autoLayout = false;
			echo json_encode( $result );
		}
		else if( $this->request->is('post') ) {
			$this->User->create();
			if( $this->User->save($this->request->data) ) {
				$this->setUserInfo(__d('admin', 'The User has been created.'), 'success');
				$this->redirect(array('action' => 'index'));
			}
			else {
				$this->log('Error creating User: ' . print_r($this->Product->validationErrors, true));
				$this->setUserInfo(__d('admin', 'The User could not be saved. Please, try again.'), 'error');
			}
		}
		
	}
	
}